//
//  投票.m
//  xinhua_sx
//
//  Created by love on 14-8-16.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "投票.h"
#import "AFNetworking.h"


#define Z @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]
@implementation toupiao
@synthesize titleLable;
@synthesize contentLabel;
@synthesize dataLable;
@synthesize hitLable;
@synthesize chousID;
@synthesize titltID;
@synthesize dicf;
@synthesize popArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithDic:(id)dic
{
    self = [super init];
    if (self)
    {
        dicf = dic;
        popArray = [[NSMutableArray alloc] init];
        self.backgroundColor =  [UIColor whiteColor];
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 100)];
        self.titleLable.text = [dic objectForKey:@"title"];
        self.titleLable.numberOfLines = 0;
        self.titleLable.backgroundColor = [UIColor clearColor];
        [self addSubview:self.titleLable];
        titltID = [dic[@"id"] stringValue];
        UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 131, 290, 1)];
        l1.backgroundColor = [UIColor blackColor];
        [self addSubview:l1];
        toupiaocheng = -1;
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 132, 290, 75)];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.text = dic[@"content"];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:self.contentLabel];
        
        self.dataLable = [[UILabel alloc] initWithFrame:CGRectMake(15, 200, 145, 15)];
        self.dataLable.font = [UIFont systemFontOfSize:12];
        self.dataLable.backgroundColor = [UIColor clearColor];
        self.dataLable.text = dic[@"createDate"];
        [self addSubview:self.dataLable];
        
        self.hitLable = [[UILabel alloc] initWithFrame:CGRectMake(160, 200, 145, 15)];
        self.hitLable.backgroundColor = [UIColor clearColor];
        self.hitLable.font = [UIFont systemFontOfSize:12];
        self.hitLable.textAlignment = NSTextAlignmentRight;
        NSNumber* net = dic[@"hits"];
        zongcoun = [net intValue];
        self.hitLable.text = [NSString stringWithFormat:@"%@人参与",net];
        [self addSubview:self.hitLable];
        NSArray* tempArray = [dic objectForKey:@"postChooseList"];
        for (int i = 0; i < [tempArray count] ; i ++)
        {
            UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(15, 220+i*60, 290, 20)];
            [button addTarget:self action:@selector(butonAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setBackgroundColor:[UIColor clearColor]];
            button.tag = [tempArray[i][@"id"] integerValue];
            [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
            UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(15, 220+i*60, 290, 20)];
            l.backgroundColor = [UIColor grayColor];
            l.font = [UIFont systemFontOfSize:13];
            l.text = [NSString stringWithFormat:@"%@.%@",tempArray[i][@"choose"],tempArray[i][@"content"]];
            NSLog(@"%@",l.text);
            [self addSubview:l];
            [self addSubview:button];
            ASProgressPopUpView* pipView = [[ASProgressPopUpView alloc] initWithFrame:CGRectMake(15, 220+i*60+55, 290,15)];
            pipView.popUpViewAnimatedColors = @[[UIColor redColor], [UIColor orangeColor], [UIColor greenColor]];
            //pipView.dataSource = self;
            //[pipView.showPopUpViewAnimated:YES];
            [self addSubview:pipView];
            pipView.font = [UIFont fontWithName:@"Futura-CondensedExtraBold" size:16];
            [pipView showPopUpViewAnimated:YES];
            [popArray addObject:pipView];
            
        }
         [self progress1];
         [self progress2];
         [self progress3];
        
    }
    return self;
}


- (void)progress1
{
   // NSLog(@"22");
    ASProgressPopUpView* tempview = [popArray objectAtIndex:0];
    float count = [[dicf objectForKey:@"hits"] floatValue] +toupiaocheng ;
    float nowC =  [[[[dicf objectForKey:@"postChooseList"] objectAtIndex:0] objectForKey:@"hits"] floatValue];
    if (toupiaocheng == 0)
    {
        nowC ++;
        // NSLog(@"1");
    }
    float progress =tempview.progress;
    if ( progress < nowC/zongcoun) {
           //NSLog(@"1 ++");
            progress +=  0.005;
            [tempview setProgress:progress animated:NO];
            //[self.progressView2 setProgress:progress animated:!_continuousButton.selected];
            [NSTimer scheduledTimerWithTimeInterval:0.05
                                             target:self
                                           selector:@selector(progress1)
                                           userInfo:nil
                                            repeats:NO];
    }
    
}
- (void)progress2
{
    
    ASProgressPopUpView* tempview = [popArray objectAtIndex:1];
    float count = [[dicf objectForKey:@"hits"] floatValue] +toupiaocheng;
    float nowC =  [[[[dicf objectForKey:@"postChooseList"] objectAtIndex:1] objectForKey:@"hits"] floatValue];
    if (toupiaocheng == 1)
    {
        nowC ++;
         //NSLog(@"22");
    }
    float progress =tempview.progress;
    if ( progress < nowC/zongcoun) {
        progress +=  0.005;
        [tempview setProgress:progress animated:NO];
        //[self.progressView2 setProgress:progress animated:!_continuousButton.selected];
        [NSTimer scheduledTimerWithTimeInterval:0.05
                                         target:self
                                       selector:@selector(progress2)
                                       userInfo:nil
                                        repeats:NO];
    }
    
}
- (void)progress3
{
   // NSLog(@"22");
    ASProgressPopUpView* tempview = [popArray objectAtIndex:2];
    float count = [[dicf objectForKey:@"hits"] floatValue] +toupiaocheng;
    float nowC =  [[[[dicf objectForKey:@"postChooseList"] objectAtIndex:2] objectForKey:@"hits"] floatValue];
    if (toupiaocheng == 1)
    {
        nowC ++;
         //NSLog(@"3");
    }
    float progress =tempview.progress;
    if ( progress < nowC/zongcoun) {
        progress +=  0.005;
        [tempview setProgress:progress animated:NO];
        //[self.progressView2 setProgress:progress animated:!_continuousButton.selected];
        [NSTimer scheduledTimerWithTimeInterval:0.05
                                         target:self
                                       selector:@selector(progress3)
                                       userInfo:nil
                                        repeats:NO];
    }
    
}
-(void)butonAction:(UIButton*)button
{
    butoncl* al = [[butoncl alloc] init];
    [al postString:@"013"fff:@""];
    NSString* str = [NSString stringWithFormat:@"%d",button.tag];
    NSString* UID = [[NSUserDefaults standardUserDefaults] objectForKey:@"UID"];
    if (!UID)
    {
        UIAlertView* viw = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [viw show];
        return ;
    }
    HUD = [[MBProgressHUD alloc] initWithView:self];
    [self addSubview:HUD];
    HUD.dimBackground = YES;
    HUD.labelText = @"正在加载";
    [HUD show:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSDictionary* dic =@{@"postChooseId":str,@"postTitleId":titltID,@"userId":UID};
    NSString* sting = [NSString stringWithFormat:@"http://%s/xinhua/m/setPostChooseHits.html",HTTP];
    [manager POST:sting parameters:dic success:^(AFHTTPRequestOperation*operation, id responseObject) {
        id jsonObject = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
        //[HUD hide:YES];
        
       // NSLog(@"json = %@",jsonObject);
        NSString* string = [NSString stringWithFormat:@"%@",[jsonObject objectForKey:@"result"]];
        if([string isEqualToString:@"success"])
        {
            NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
            //NSDictionary* dic = [NSDictionary di]
            UIAlertView* view1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
           // [view1 show];
            HUD.labelText = @"投票成功";
            //[HUD show:YES];
            sleep(2.5);
            [HUD hide:YES];
        
            NSString* fg = button.titleLabel.text;
            toupiaocheng = [fg intValue];
            zongcoun ++;
            self.hitLable.text = [NSString stringWithFormat:@"%d人参与",zongcoun];
            //[view setFF];
            [self progress1];
            [self progress2];
            [self progress3];
           
            
            
        }
        else
        {
            HUD.labelText = jsonObject[@"message"];
            //[HUD show:YES];
            sleep(2.5);
            [HUD hide:YES];
            UIAlertView* view1 = [[UIAlertView alloc] initWithTitle:@"提示" message:jsonObject[@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            // [view1 show];
           // [self progress1];
           // [self progress2];
           // [self progress3];
            
            
        }
        
    }
          failure:^(AFHTTPRequestOperation *operation, NSError*error) {
              [HUD hide: YES];
             // NSLog(@"error = %@",error);
              
          }];

}
-(void)setFF
{
    
}
- (NSString *)progressView:(ASProgressPopUpView *)progressView stringForProgress:(float)progress
{
    NSString *s;
    if (progress < 0.2) {
        s = @"Just starting";
    } else if (progress > 0.4 && progress < 0.6) {
        s = @"About halfway";
    } else if (progress > 0.75 && progress < 1.0) {
        s = @"Nearly there";
    } else if (progress >= 1.0) {
        s = @"Complete";
    }
    return s;
}

// by default ASProgressPopUpView precalculates the largest popUpView size needed
// it then uses this size for all values and maintains a consistent size
// if you want the popUpView size to adapt as values change then return 'NO'
- (BOOL)progressViewShouldPreCalculatePopUpViewSize:(ASProgressPopUpView *)progressView;
{
    return NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
